package com.bbs.db.impl;

import com.bbs.db.iface.PostinfoDao;
import com.bbs.db.pers.Postinfo;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * @time Nov 12, 2009, 9:22:02 PM
 * @author fdcube
 */
public class PostinfoImpl extends HibernateDaoSupport implements PostinfoDao {

    public boolean AddPost(Postinfo value) {
        try {
            this.getHibernateTemplate().merge(value);
            return true;
        } catch (DataAccessException dataAccessException) {
            logger.error(dataAccessException);
            return false;
        }
    }

    public boolean DeletePost(Postinfo value) {
        try {
            Postinfo ptmp = this.getHibernateTemplate().get(Postinfo.class, value.getPid());
            this.getHibernateTemplate().delete(ptmp);
            return true;
        } catch (DataAccessException dataAccessException) {
            logger.error(dataAccessException);
            return false;
        }
    }

    public List<Postinfo> FindPosts() {
        return this.getHibernateTemplate().find("FROM Postinfo ORDER BY pid DESC");
    }

    public boolean UpdatePost(Postinfo value) {
        try {
            this.getHibernateTemplate().merge(value);
            return true;
        } catch (DataAccessException dataAccessException) {
            logger.error(dataAccessException);
            return false;
        }
    }

    public List<Postinfo> FindPost(Postinfo value) {
        return this.getHibernateTemplate().findByNamedParam("FROM Postinfo as p WHERE p.pid =:pid ORDER BY pid DESC","pid",value.getPid());
    }
    public List<Postinfo> FindPostByUser(Postinfo value) {
        return this.getHibernateTemplate().findByNamedParam("FROM Postinfo as p WHERE p.userinfo =:userinfo", "userinfo", value.getUserinfo());
    }
}
